package com.adminx.web.controller;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.adminx.po.Category;
import com.adminx.po.News;
import com.adminx.po.User;
import com.adminx.service.CategoryService;
import com.adminx.service.NewsService;
import com.adminx.service.impl.CategoryServiceImpl;
import com.adminx.service.impl.NewsServiceImpl;
import com.adminx.utils.IdGenerator;
import com.adminx.utils.ModelAndView;
import com.adminx.utils.PageBean;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/*
 * 新闻控制类
 */
@WebServlet("/adminx/news")
public class NewsController extends CommonController {
	private Logger logger = LogManager.getLogger(NewsController.class) ;
	//依赖注入
	private NewsService newsService = new NewsServiceImpl() ;
	private CategoryService categoryService = new CategoryServiceImpl();


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String action = req.getParameter("action");
			logger.debug("newsController action: {}", action) ;
			switch (action) {
				case "add":
					addNews(req, resp);
					break;
				case "findByPage":
					findNewsByPage(req, resp);
					break;
				default:
					break;
			}
		} catch (Exception e) {
			logger.error("{}", e.getMessage()) ;
			ModelAndView model = ModelAndView.buildFailed(resp, "内部处理出错") ;
			model.response();
		}
	}

	private void addNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// preparing parameters of news
		logger.debug("enter addNews action. ") ;
		News  news = this.readRequestData(req, News.class) ;
		if (news == null) {
			logger.debug("no received data. ") ;
		}
		news.setPublishTime(new Date());
		HttpSession session = req.getSession() ;
		User user = (User) session.getAttribute("login_user") ;
		news.setUserId(user.getUserId()) ;
		news.setClicks(0) ;
		news.setNewsId(IdGenerator.newId()) ;
		//processing the request
		logger.debug("addNews action and got params : {}. ", news.toString()) ;
		int ret = newsService.addNews(news) ;
		// returning result.
		ModelAndView retModel = null ;
		if (ret > 0) {
			// successfully
			retModel = ModelAndView.buildSuccess(resp) ;
		} else {
			// failed
			retModel = ModelAndView.buildFailed(resp) ;
		}
		logger.debug("addNews-action finished.") ;
		retModel.response();
	}

	//查询新闻分页
	private void findNewsByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String currentPageStr = req.getParameter("page") ;
		String pageSizeStr = req.getParameter("limit") ;
		logger.debug("findNewsByPage parameters: {} {}", currentPageStr, pageSizeStr) ;
		Integer currentPage = Integer.parseInt(currentPageStr);
		Integer pageSize = Integer.parseInt(pageSizeStr);
		String keywords = req.getParameter("keywords") ;


		ModelAndView model = null ;
		try {
			// 获取角色列表
			List<Category> categoryList = categoryService.findCategoryList();

			//model.addAttribute("categoryList", categoryList);
			// 获取用户PageBean实例
			PageBean<News> pb = newsService.findNewsByPage(keywords, null, currentPage, pageSize);
			model = ModelAndView.buildSuccess(resp);
			model.addAttribute("msg","good" )
					.addAttribute("data", pb.getList())
					.addAttribute("count", pb.getCount()) ;
			model.response();
		} catch (Exception e) {
			model = ModelAndView.buildFailed(resp, "data processed failed.") ;
			model.response();
		}
	}
	//设置新闻的状态（publishStatus：1：发布；2：撤稿）
/*	@RequestMapping(value = "/setNewsPublishStatus.action")
	@ResponseBody*/
/*	public News setNewsPublishStatus(@RequestBody News news, Model model) {
		int rows = newsService.setNewsPublishStatus(news);
		if (rows>0) {
			return news;
		}else{
			news.setNewsId(0);
			return news;
		}
	}
	//转向添加新闻
	@RequestMapping(value = "/toAddNews.action")
	public String toAddNews(Model model) {
		// 获取新闻类别列表
		List<Category> categoryList = categoryService.findCategoryList();
		model.addAttribute("categoryList", categoryList);
		return "news/add_news";
	}
	// 添加新闻
	@RequestMapping(value = "/addNews.action", method = RequestMethod.POST)
	public String addNews(News news, Model model) {
		Date date = new Date();
		news.setPublishTime(date);
		news.setPublishStatus("1");// 默认设置新闻为已发布状态
		int rows = newsService.addNews(news);
		// 添加成功，转向用户列表页面
		if (rows > 0) {
			return "redirect:findNewsByPage.action";
		} else {
			// 获取新闻类别列表
			List<Category> categoryList = categoryService.findCategoryList();
			model.addAttribute("categoryList", categoryList);
			model.addAttribute("news", news);
			// 添加失败，转回添加用户页面
			return "news/add_news";
		}
	}
	// 修改新闻
	@RequestMapping(value = "/editNews.action", method = RequestMethod.POST)
	public String editNews(News news, Model model) {
		Date date = new Date();
		news.setPublishTime(date);
		news.setPublishStatus("1");// 默认设置新闻为已发布状态
		int rows = newsService.editNews(news);
		// 添加成功，转向用户列表页面
		if (rows > 0) {
			return "redirect:findNewsByPage.action";
		} else {
			// 获取新闻类别列表
			List<Category> categoryList = categoryService.findCategoryList();
			model.addAttribute("categoryList", categoryList);
			model.addAttribute("news", news);
			// 添加失败，转回添加用户页面
			return "news/edit_news";
		}
	}
	// 转向修改新闻页面
	@RequestMapping(value = "/toEditNews.action")
	public String toEditNews(Integer newsId, Model model) {
		News news = newsService.getNewsByNewsId(newsId);
		if (news != null) {
			model.addAttribute("news", news);
			// 获取新闻类别列表
			List<Category> categoryList = categoryService.findCategoryList();
			model.addAttribute("categoryList", categoryList);
		}
		return "news/edit_news";
	}
	//删除新闻
	@RequestMapping(value = "/delNews.action")
	@ResponseBody
	public News delNews(@RequestBody News news, Model model) {
		int rows = newsService.delNews(news.getNewsId());
		if (rows>0) {
			return news;
		}else{
			news.setNewsId(0);
			return news;
		}
	}
	//根据新闻类别ID查询新闻分页（用于前台首页）
	@RequestMapping(value = "/index.action")
	public String index(HttpServletRequest request, HttpServletResponse response, String keywords, Integer newsListCategoryId,	@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize,	Model model) throws ServletException, IOException {
		// 获取用户PageBean实例
		PageBean<News> pb1 = newsService.findNewsByPage(keywords, 1, currentPage, pageSize);
		model.addAttribute("pb1", pb1);
		PageBean<News> pb2 = newsService.findNewsByPage(keywords, 2, currentPage, pageSize);
		model.addAttribute("pb2", pb2);
		return "../../first";
	}
	//根据新闻类别ID查询新闻分页（用于前台新闻列表页）
	@RequestMapping(value = "/findNewsByCategoryIdPage.action")
	public String findNewsByCategoryIdPage(HttpServletRequest request, HttpServletResponse response, String keywords, Integer newsListCategoryId,	@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "1") Integer pageSize,	Model model){
		// 获取角色列表
		Category category = categoryService.findCategoryById(newsListCategoryId);
		model.addAttribute("category", category);
		// 获取用户PageBean实例
		PageBean<News> pb = newsService.findNewsByPage(keywords, newsListCategoryId, currentPage, pageSize);
		model.addAttribute("pb", pb);
		model.addAttribute("newsListCategoryId", newsListCategoryId);
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("pageSize", pageSize);
		return "../../list";
	}
	//查询新闻（用于前台新闻内容页）
	@RequestMapping(value = "/findFrontNewsByNewsId.action")
	public String findFrontNewsByNewsId(Integer newsId,Model model) {
		News news = newsService.getNewsByNewsId(newsId);
		if (news != null) {
			model.addAttribute("news", news);
		}
		return "../../detail";
	}*/
}